<?php
$listproj_url = elgg_get_site_url () . "submana/listpro/$group_guid";
$members = array ();
$is_error = false;
if (! can_write_to_container ( elgg_get_logged_in_user_guid (), $group_guid )) {
	system_message ( "Bạn cần tham gia vào nhóm mới có thể đăng kí đề tài" );
	forward ( $listproj_url );
	exit ();
}
for($i = 0; $i < $total_member; $i ++) {
	// add student to project
	$student_id = get_input ( "member" . ($i + 1), $DEFAULT );
	
	if ($student_id != $DEFAULT) {
		if ($i != 0) {
			$student = get_user_by_username ( $student_id );
		} else {
			$student = elgg_get_logged_in_user_entity ();
		}
		
		if ($student) {
			
			array_push ( $members, "$student->username" );
			$j ++;
		} else {
			$is_error = true;
			register_error ( elgg_echo ( 'submana:project:noneuser', array (
					"$student_id" 
			) ) );
			forward ( REFERER );
			exit ();
		}
	} else {
	}
}
if (! $is_error) {
	// $count = count($members);
	
	// Da co nhom dang ki nhung chua het slot
	$project = get_entity ( $project_guid );
	if (is_array ( $members )) {
		foreach ( $members as $student_username ) {
			$student = get_user_by_username ( $student_username );
			if ($student->classes) {
				if (strpos ( $student->classes, "$project->group_project`$project->semester`$project->academic_year" ) !== false) {
					
					$student->classes .= ",$project->group_project`$project->semester`$project->academic_year";
				}
			} else {
				$student->classes = "$project->group_project`$project->semester`$project->academic_year";
			}
			$student->save ();
		}
	} else {
		$student = get_user_by_username ( $members );
		if ($student->classes) {
			if (strpos ( $student->classes, "$project->group_project`$project->semester`$project->academic_year" ) !== false) {
				
				$student->classes .= ",$project->group_project`$project->semester`$project->academic_year";
			}
		} else {
			$student->classes = "$project->group_project`$project->semester`$project->academic_year";
		}
		$student->save ();
	}
	
	// $project_wait_guid = -1;
	if ($project->wait_project) {
		$project_wait_guid = $project->guid;
		if (is_array ( $project->students )) {
			foreach ( $project->students as $student ) {
				array_push ( $members, "$student" );
			}
		} else {
			array_push ( $members, $project->students );
		}
	}
	if ($project) {
		if (isset ( $project_wait_guid )) {
			$newproject = get_entity ( $project_wait_guid );
		} else {
			$newproject = new ElggObject ();
		}
		
		$newproject->subtype = "project";
		$newproject->title = "Chờ để gọp nhóm";
		$newproject->description = "Danh sách chờ để gọp nhóm vì chưa đủ số lượng";
		$newproject->max_group = 1;
		$newproject->current_group = 0;
		$newproject->group_project = $project->group_project;
		// $newproject->max_student = $project->max_student;
		// $newproject->min_student = $project->min_student;
		$newproject->deadline = $project->deadline;
		$newproject->container_guid = $project->container_guid;
		$newproject->projectlist_guid = $project->projectlist_guid;
		$newproject->access_id = ACCESS_LOGGED_IN;
		$newproject->semester = $project->semester;
		$newproject->academic_year = $project->academic_year;
		$newproject->students = $members;
		
		$newproject->wait_project = true;
		$newproject->save ();
	}
} else {
	register_error ( "Có lỗi xảy ra, đề nghị đăng kí lại" );
}

forward ( $listproj_url );
exit ();